package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 文件接收MultipartFile来接收
 * @author stone
 * @date 2022/06/21 16:16
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("file")
    public BaseRespVo uploadFile(MultipartFile file) throws IOException {
        // 获得上传的文件的信息
        String name = file.getName(); // 请求参数名 → file
        String originalFilename = file.getOriginalFilename(); //原始文件名
        long size = file.getSize(); // 文件的大小
        String contentType = file.getContentType(); // 正文类型 → 文件类型
        // MultipartFile.transferTo
        File saveFile = new File("D:\\WorkSpace\\j40_workspace", originalFilename);
        //File saveFile = new File("D:\\WorkSpace\\j40_workspace\\dlrb.jpg");
        file.transferTo(saveFile);
        return BaseRespVo.ok();
    }

    @RequestMapping("files")
    public BaseRespVo uploadFiles(MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename(); //原始文件名
            File saveFile = new File("D:\\WorkSpace\\j40_workspace", originalFilename);
            //File saveFile = new File("D:\\WorkSpace\\j40_workspace\\dlrb.jpg");
            file.transferTo(saveFile);
        }

        return BaseRespVo.ok();
    }
}
